var app = new Vue({
    el:"#app",
    data:{
        //列表数据
        entityList:[],
        //总记录数
        total:0,
        //页号
        pageNum:1,
        //页大小
        pageSize:10,
        //实体
        entity:{},
        //选择了的品牌id
        ids:[],
        //搜索条件对象
        searchEntity:{},
        //全选
        checkAll:false
    },
    methods:{
        //全选
        selectAll:function(){
            if(!this.checkAll){
                //选上
                this.ids = [];
                for (let i = 0; i < this.entityList.length; i++) {
                    const entity = this.entityList[i];
                    this.ids.push(entity.id);
                }
            } else {
                this.ids = [];
            }
        },
        searchList:function (pageNum) {
            this.pageNum = pageNum;

            /*
                            axios.get("../brand/findPage.do?pageNum=" + this.pageNum + "&pageSize="+this.pageSize).then(function (response) {
                                //总记录数
                                app.total = response.data.total;
                                app.entityList = response.data.list;
                            });
            */
            axios.post("../brand/search.do?pageNum=" + this.pageNum + "&pageSize="+this.pageSize, this.searchEntity).then(function (response) {
                //总记录数
                app.total = response.data.total;
                app.entityList = response.data.list;
            });
        },
        deleteList: function(){
            //判断是否有选择
            if (this.ids.length == 0) {
                alert("请先选择要删除的记录");
                return;
            }
            //确认是否要删除；confirm 当点击弹出来的框中的确定时返回true否则false
            if (confirm("你确认要删除选择了的记录吗？")) {
                //删除
                axios.get("../brand/delete.do?ids=" + this.ids).then(function (response) {
                    if(response.data.success){
                        app.ids = [];
                        app.searchList(1);
                    } else {
                        alert(response.data.message);
                    }
                });
            }


        },
        //根据id查询
        findOne: function(id){
            axios.get("../brand/findOne/" + id + ".do").then(function (response) {
                app.entity = response.data;
            });
        },
        //保存
        save: function () {
            var method = "add";

            if(this.entity.id != null){
                //修改
                method = "update";
            }

            axios.post("../brand/"+method+".do", this.entity).then(function (response) {
                if(response.data.success){
                    //刷新列表
                    app.searchList(app.pageNum);
                } else {
                    alert(response.data.message);
                }
            });
        }

    },
    //监控，vue实例的数据属性
    watch:{
        ids:{
            //开启深度监控
            deep:true,
            handler:function (newValue, oldValue) {
                if (this.ids.length == this.entityList.length) {
                    this.checkAll = true;
                } else {
                    this.checkAll = false;
                }
            }
        }
    },
    created(){
        /*axios.get("../brand/findAll.do").then(function (response) {
            //response里面的属性有：data,status,statusText,headers,config
            console.log(response);
            //this表示窗口对象，期望的是vue实例,app就是vue实例变量
            app.entityList = response.data;
        }).catch(function () {
            //如果网络中断，服务器宕机
            alert("获取数据失败!");
        });*/
        this.searchList(this.pageNum);
    }
});
